#!/usr/bin/env python
# -*- coding: utf-8 -*-
#=======================================#
# @author: RedSoxFan and NerfSoldier
# @copyright: 2012 RedSoxFan and NerfSoldier
# @license: GNU General Public License v3.0 
#           <http://www.gnu.org/copyleft/gpl.html>
# @summary: Setup File
#=======================================#

import sys
if len(sys.argv)==1: sys.argv.append("py2exe")
from distutils.core import setup
import py2exe
import os
import shutil
def clean_folder(path='.'):
    for f in os.listdir(path):
        if not f: continue
        if os.path.isdir(os.path.join(path,f)):
            if f in ["build","dist"]: shutil.rmtree(os.path.join(path,f)) 
            else: clean_folder(os.path.join(path,f))
        elif f.endswith(".pyc"): os.remove(os.path.join(path,f))
        elif f.startswith("~") or f.endswith("~"): os.remove(os.path.join(path,f))
def list_dir(path=".."):
    l = []
    for f in os.listdir(path):
        if not f or f.startswith(".") or f.endswith("~") or f.startswith("~"): continue;
        if os.path.isdir(os.path.join(path,f)): l+=list_dir(os.path.join(path,f))
        elif f=="library.zip" or (not f.endswith(".zip") and not f.endswith(".pyc") and not f.endswith(".cache")): 
            l.append(os.path.join(path,f))
    return l
from darkdev import DarkDev
ddver = DarkDev.VERSION
if os.path.exists("../build"): shutil.rmtree("../build") # Remove Old Build Folder
clean_folder() 
setup(
    name = 'DarkDev',
    description = 'DarkDev IDE',
	author = "RedSoxFan and NerfSoldier",
    version = ddver.replace("-",".").replace("devel","0").replace("alpha","1").replace("beta","2").replace("release","3"),

	data_files = ["COPYING",
                ],
    windows = [
                  {
                      'script': 'darkdev.py',
					  "icon_resources": [(1, 'resources/icon/icon.ico')],
                  }
              ],
	zipfile = "pylib/library.zip",
    options = {
                  'py2exe': {
                      'packages':'encodings',
                      'includes': 'editor, ddlogger, workspace, project, sip',
					  'excludes':'parsers, templates, resources',
					  "dll_excludes": ["MSVCP90.dll","w9xpopen.exe"],
					  'dist_dir':'../build/',
					  'bundle_files':3,
                  }
              },
)
for folder in ["resources","parsers","templates"]:
    shutil.copytree(os.path.abspath(folder), os.path.abspath("..\\build\\%s"%folder), ignore=lambda s,n:[f for f in n if f[0] is "."]) # Copy Resources Folder
clean_folder()
ver = "%s%s"%(sys.version_info.major,sys.version_info.minor)    
if os.path.exists("../build/DarkDev.exe"):
    if not os.path.exists("../build/python%s.dll"%ver):
        shutil.copyfile(os.path.join(os.path.dirname(sys.executable),"python%s.dll"%ver), "../build/python%s.dll"%ver)
    if not os.path.exists("../build/pylib/QtCore4.dll"):
        shutil.copyfile(os.path.join(os.path.dirname(sys.executable),"Lib","site-packages","PyQt4","QtCore4.dll"),"../build/pylib/QtCore4.dll")
    if not os.path.exists("../build/pylib/QtGui4.dll"):
        shutil.copyfile(os.path.join(os.path.dirname(sys.executable),"Lib","site-packages","PyQt4","QtGui4.dll"),"../build/pylib/QtGui4.dll")

from zipfile import ZipFile, ZIP_DEFLATED
with ZipFile('../DarkDev-%s.zip'%ddver, 'w', ZIP_DEFLATED) as myzip:
    for f in list_dir():
        myzip.write(f,f.replace("..\\",""))
clean_folder()
if os.path.exists("../../../upload_darkdev"):
    x = raw_input("Would You Like To Upload Zip To Google Code? ")
    if x.lower()=="y":
        os.system("../../../upload_darkdev \"%s\" \"%s\""%(os.path.abspath('../DarkDev-%s.zip'%ddver).replace("\\","/")[2:],"Windows and Source Build for %s"%('DarkDev-%s'%ddver)))
        raw_input("")